/**
* ...
* @author Ankur Arora
* @version 1.0
*/

import mx.video.FLVPlayback;


class classes.utils.VideoDownloader
{
	
	
	private var m_arrVideoList:Array;
	private var m_FLVDownloader:FLVPlayback;
	private var m_intCurrentDonwloadIndex:Number;
	
	public function VideoDownloader(FLVDownloader:FLVPlayback)
	{
		m_arrVideoList = new Array();
		m_FLVDownloader = FLVDownloader;
		m_FLVDownloader._visible = false;
		m_intCurrentDonwloadIndex = 0;
		createFLVEvents();
	}
		
	public function startDownloadingList():Void 
	{
		if(m_intCurrentDonwloadIndex < m_arrVideoList.length)
		{
			if(m_FLVDownloader.contentPath != "")
				m_FLVDownloader.stop();
				
			m_FLVDownloader.contentPath = m_arrVideoList[m_intCurrentDonwloadIndex].videoFile;
		}
			
		//trace("m_FLVDownloader " + m_FLVDownloader.contentPath)
	}
	
	public function addVideoToQueue(strVideoPath:String):String 
	{
		m_arrVideoList.push({videoFile: strVideoPath, isDownloaded:false});
		return "Video added to the Queue for downloading";
	}
	
	private function createFLVEvents()
	{
		var objThis = this;
		var eventObject:Object = new Object();
		eventObject.progress = function(e:Object)
		{
			if(e.bytesLoaded == e.bytesTotal)
			{
				objThis.m_arrVideoList[m_intCurrentDonwloadIndex].isDownloaded = true;
				objThis.m_intCurrentDonwloadIndex++;
				objThis.startDownloadingList();
			}
		}
		m_FLVDownloader.addEventListener("progress", eventObject);
	}
	
	private function searchForItemIndex(strSearchString:String):Number 
	{
		for (var i:Number=0; i<m_arrVideoList.length; i++) 
		{
			if(strSearchString == m_arrVideoList[i].videoFile && m_arrVideoList[i].isDownloaded == false)
			{
				return i;
			}
		}
		return -1;
	}
	
	
	
	/*
	public function removeVideoFromQueue(strVideoPath:String):String 
	{
		var intItemIndex = searchForItemIndex(strVideoPath);

		if(intItemIndex == -1)
		{
			return "Video is not avialable in list or already downloaded";
		}
		else
		{
			m_arrVideoList.splice(intItemIndex,1);
			return "Video removed from the Queue fow downloading";
		}
	}
	*/
	
	
}